package activity.login;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;

import activity.messge.ActivityMessge;
import activity.personal.ActivityPerSonal;
import base.BaseActivity;
import base.MyApplcation;
import fragment.OrderFragemnt;
import fragment.OrdersFragemnt;
import logincontroller.LoginController;
import logincontroller.LoginedState;
import logincontroller.UnLoginState;
import util.MyPermission;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.utls.UIAlertView;


public class MainActivity extends BaseActivity {

    TextView btn_head_a, btn_head_b;
    List<Fragment> fragments = new ArrayList<>();
   public  ViewPager viewPager;
    private ImageView imgMessge, imgProalin;
    UIAlertView uiAlertView;
    Button noBtnLong;
    public static MainActivity act;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyPermission.CheckPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
        act = this;
        /**
         * 登录状态
         */
        if ((Boolean) SPUtils.get(MyApplcation.ctx, null,
                Const.IS_LOGINED, false)) {
            LoginController.setLoginState(new LoginedState());
            Log.v("logined", "已经登录");
        } else {
            LoginController.setLoginState(new UnLoginState());
            Log.v("logined", "未登录");
        }
        init();
        OrdersFragemnt repairFragment = new OrdersFragemnt();
        OrderFragemnt orderTvFragment = new OrderFragemnt();
        setStatusBarColor(R.color.main_color);
        fragments.add(repairFragment);
        fragments.add(orderTvFragment);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setCurrentItem(0);
        changeTextBG(0);
        btn_head_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("btn_head_b", "b");
                viewPager.setCurrentItem(1);
            }
        });
        btn_head_a.setOnClickListener(this);

        viewPager.setOnPageChangeListener(new myPagerListener());
        IntentFilter filter1 = new IntentFilter("chaingLogin");
        registerReceiver(nichengBroadcastReceiver, filter1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nichengBroadcastReceiver);
    }

    public void init(){
        if (IsUtilUid.isUid() == null || IsUtilUid.isUid() == "") {
            viewPager.setVisibility(View.GONE);
            noBtnLong.setVisibility(View.VISIBLE);
            noBtnLong.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, ActivityLogin.class);
                    startActivity(intent);
                }
            });
        } else {
            noBtnLong.setVisibility(View.GONE);
            viewPager.setVisibility(View.VISIBLE);
        }
    }
    public static MainActivity onSetFragment() {
        return act;

    }

    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            viewPager.setVisibility(View.VISIBLE);
            noBtnLong.setVisibility(View.GONE);
        }

    };

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {

            case R.id.btn_head_a:
                viewPager.setCurrentItem(0);
                Log.d("btn_head_a", "a");
                break;
            case R.id.btn_head_b:
                Log.d("btn_head_b", "b");
                viewPager.setCurrentItem(1);
                break;
            case R.id.imgProalin://个人中心

                intent = new Intent(MainActivity.this, ActivityPerSonal.class);
                startActivity(intent);
                break;
            case R.id.imgMessge://消息
                intent = new Intent(MainActivity.this, ActivityMessge.class);
                startActivity(intent);
                break;

        }
    }

    private void changeTextBG(int position) {
        switch (position) {
            case 0:
                btn_head_a.setBackgroundResource(R.drawable.house_rent_selected);
                btn_head_b.setBackgroundColor(getResources().getColor(R.color.gray_divider));
                btn_head_a.setTextColor(getResources().getColor(R.color.white));
                btn_head_b.setTextColor(getResources().getColor(R.color.black));
                break;
            case 1:
                btn_head_b.setTextColor(getResources().getColor(R.color.white));
                btn_head_a.setTextColor(getResources().getColor(R.color.black));
                btn_head_b.setBackgroundResource(R.drawable.house_center_selected);
                btn_head_a.setBackgroundColor(getResources().getColor(R.color.gray_divider));
                break;
        }

    }

    class myPagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.setCurrentItem(position);
            changeTextBG(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }


    }


    class MyPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragments;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    private long exitTime = 0;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
//            if((System.currentTimeMillis()-exitTime) > 2000){
//               ByAlert.alert("再按一次退出程序");
//                exitTime = System.currentTimeMillis();
//            } else {
//                //遍历所有activity并推出
//                System.exit(0);
//            }
            showDelDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showDelDialog() {
        uiAlertView = new UIAlertView(MainActivity.this, "温馨提示！", "确认是否退出", "否", "是");
        uiAlertView.show();

        uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {

            public void doLeft() {
                uiAlertView.dismiss();
            }

            public void doRight() {
                uiAlertView.dismiss();
                finish();
//                System.exit(0);

            }
        });
    }


}
